import { ContactType, AddOrUpdateContactQueryType } from "../service/contact";

// Mock数据
const mockContactList: ContactType[] = [
  {
    id: "1",
    companyName: "张三",
    contactName: "138****8888",
    contactPhone: "zhangsan@example.com",
    deviceId: "1",
  },
  {
    id: "2",
    companyName: "李四",
    contactName: "139****9999",
    contactPhone: "lisi@example.com",
    deviceId: "1",
  },
  {
    id: "3",
    companyName: "王五",
    contactName: "186****6666",
    contactPhone: "wangwu@example.com",
    deviceId: "1",
  },
  {
    id: "4",
    companyName: "赵六",
    contactName: "152****5555",
    contactPhone: "zhaoliu@example.com",
    deviceId: "1",
  },
  {
    id: "5",
    companyName: "孙七",
    contactName: "177****7777",
    contactPhone: "sunqi@example.com",
    deviceId: "1",
  },
];

// 生成唯一ID
const generateId = (): string => {
  return Date.now().toString() + Math.random().toString(36).substr(2, 9);
};

// 模拟延迟
const delay = (ms = 300): Promise<void> => {
  return new Promise((resolve) => setTimeout(resolve, ms));
};

// 获取联系人列表的mock
export const mockGetContactList = async (): Promise<{
  status: number;
  data: ContactType[];
  message: string;
}> => {
  await delay();

  return {
    status: 0,
    data: mockContactList,
    message: "获取联系人列表成功",
  };
};

// 添加或更新联系人的mock
export const mockAddOrUpdateContact = async (
  params: AddOrUpdateContactQueryType
): Promise<{ status: number; data: ContactType | null; message: string }> => {
  await delay();

  try {
    // 编辑联系人
    if (params.id) {
      const index = mockContactList.findIndex(
        (contact) => contact.id === params.id
      );

      if (index === -1) {
        return {
          status: 1,
          data: null,
          message: "联系人不存在",
        };
      }

      // 更新联系人信息
      const updatedContact: ContactType = {
        ...mockContactList[index],
        ...Object.fromEntries(
          Object.entries(params).filter(
            ([_, value]) => value !== null && value !== undefined
          )
        ),
      } as ContactType;

      mockContactList[index] = updatedContact;

      return {
        status: 0,
        data: updatedContact,
        message: "更新联系人成功",
      };
    }
    // 添加新联系人
    else {
      // 验证必填字段
      if (!params.companyName || !params.contactPhone) {
        return {
          status: 1,
          data: null,
          message: "姓名和电话为必填项",
        };
      }

      // 检查电话是否已存在
      const existingContact = mockContactList.find(
        (contact) => contact.contactPhone === params.contactPhone
      );
      if (existingContact) {
        return {
          status: 1,
          data: null,
          message: "该电话号码已存在",
        };
      }

      const newContact: ContactType = {
        id: generateId(),
        companyName: params.companyName || "",
        contactName: params.contactName || "",
        contactPhone: params.contactPhone || "",
        deviceId: params.deviceId || "",
      };

      mockContactList.unshift(newContact); // 添加到列表开头

      return {
        status: 0,
        data: newContact,
        message: "添加联系人成功",
      };
    }
  } catch (error) {
    return {
      status: 1,
      data: null,
      message: "操作失败，请重试",
    };
  }
};

// 删除联系人的mock（可选，如果需要的话）
export const mockDeleteContact = async (
  id: string
): Promise<{ status: number; data: null; message: string }> => {
  await delay();

  const index = mockContactList.findIndex((contact) => contact.id === id);

  if (index === -1) {
    return {
      status: 1,
      data: null,
      message: "联系人不存在",
    };
  }

  mockContactList.splice(index, 1);

  return {
    status: 0,
    data: null,
    message: "删除联系人成功",
  };
};

// 根据ID获取单个联系人的mock（可选）
export const mockGetContactById = async (
  id: string
): Promise<{ status: number; data: ContactType | null; message: string }> => {
  await delay();

  const contact = mockContactList.find((contact) => contact.id === id);

  if (!contact) {
    return {
      status: 1,
      data: null,
      message: "联系人不存在",
    };
  }

  return {
    status: 0,
    data: contact,
    message: "获取联系人详情成功",
  };
};
